package schoolWork.work06.test4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MDIWindow extends JFrame implements ActionListener {

    JDesktopPane desk;
    JMenuBar menuBar;
    JMenu menu;
    JMenuItem itemNew, itemCopy, itemCut, itemPaste;

    MDIWindow() {
        desk = new JDesktopPane();
        desk.setDesktopManager(new DefaultDesktopManager());
        add(desk, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        menuBar = new JMenuBar();
        menu = new JMenu("编辑");
        itemNew = new JMenuItem("新建");
        itemCopy = new JMenuItem("复制");
        itemCut = new JMenuItem("剪切");
        itemPaste = new JMenuItem("粘贴");
        itemNew.addActionListener(this);
        itemCopy.addActionListener(this);
        itemCut.addActionListener(this);
        itemPaste.addActionListener(this);
        menu.add(itemNew);
        menu.add(itemCopy);
        menu.add(itemCut);
        menu.add(itemPaste);
        menuBar.add(menu);
        setJMenuBar(menuBar);
        setBounds(100, 100, 300, 300);
        setVisible(true);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == itemNew) {
            JInternalFrame a[] = desk.getAllFrames();
            for (int i = 0; i < a.length; i++) {
                desk.setLayer(a[i], JDesktopPane.DEFAULT_LAYER);
            }
            JInternalFrame newInternalFrame = new MyInternalFrame("无标题");
            newInternalFrame.setBounds(10, 10, 300, 300);
            newInternalFrame.setVisible(true);
            desk.add(newInternalFrame, JDesktopPane.DRAG_LAYER);
        }
        if (e.getSource() == itemCopy) {
            MyInternalFrame internalFrame = (MyInternalFrame) desk.getSelectedFrame();
            JTextArea text = internalFrame.getJTextArea();
            text.copy();
        } else if (e.getSource() == itemCut) {
            MyInternalFrame internalFrame = (MyInternalFrame) desk.getSelectedFrame();
            JTextArea text = internalFrame.getJTextArea();
            text.cut();
        }else if(e.getSource() == itemPaste){
            MyInternalFrame internalFrame = (MyInternalFrame) desk.getSelectedFrame();
            JTextArea text = internalFrame.getJTextArea();
            text.paste();
        }
    }
}
